/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年12月7日
 * @version 0.1
 */
package com.suredy.app.detection.service;

import java.util.LinkedHashMap;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.app.detection.entity.TurbidityEntity;
import com.suredy.core.service.BaseSrvWithEntity;


/**
 * @author ZhangMaoren
 *
 */
@Service("TurbiditySrv")
public class TurbiditySrv extends BaseSrvWithEntity<TurbidityEntity>{
	@Override
	public DetachedCriteria getDc(TurbidityEntity t) {
		DetachedCriteria dc = super.getDc(t);
		if (t == null) {
			return dc;
		}
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		if (!StringUtils.isBlank(t.getDetector())) {
			dc.add(Restrictions.eq("detector", t.getDetector()));
		
		}
		if (!StringUtils.isBlank(t.getAuditor())) {
			dc.add(Restrictions.eq("auditor", t.getAuditor()));
//			dc.add(Restrictions.like("code", t.getCode(), MatchMode.ANYWHERE));
		}
		return dc;
	}
	
	public List<TurbidityEntity> getAll() {
		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
		order.put("createTime", false);
		return this.readByEntity(null, order);
	}
	

	@Transactional
	public TurbidityEntity getById(String id) {
		TurbidityEntity po = this.get(id);
		if (po == null) {
			return null;
		}
		po.getItems().size();
		return po;
	}
	

	
	@Transactional
	public List<TurbidityEntity> getByDetector(String detector) {
		if (StringUtils.isBlank(detector)) {
			return null;
		}
		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
		order.put("createTime", false);
		TurbidityEntity search = new TurbidityEntity();
		search.setDetector(detector);
		List<TurbidityEntity> pos = this.readByEntity(search, order);
		if (pos != null && !pos.isEmpty()) {
			for (TurbidityEntity po : pos) {
				po.getItems().size();
			}
		}
		return pos;
	}
}
